home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / daemons / syslog_f.z / syslog_f / logger / RCS / logger.c,v next >
Encoding:
Text File  |  1993-03-17  |  5.0 KB  |  228 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 1.2
  9. date    93.03.17.20.09.47;    author negaard;    state Exp;
  10. branches;
  11. next    1.1;
  12.  
  13. 1.1
  14. date    93.03.17.20.08.15;    author negaard;    state Exp;
  15. branches;
  16. next    ;
  17.  
  18.  
  19. desc
  20. @Send a message to the syslog daemon.
  21. @
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for linux.
  27. @
  28. text
  29. @/*
  30.  * Copyright (c) 1983 Regents of the University of California.
  31.  * All rights reserved.
  32.  *
  33.  * Redistribution and use in source and binary forms, with or without
  34.  * modification, are permitted provided that the following conditions
  35.  * are met:
  36.  * 1. Redistributions of source code must retain the above copyright
  37.  *    notice, this list of conditions and the following disclaimer.
  38.  * 2. Redistributions in binary form must reproduce the above copyright
  39.  *    notice, this list of conditions and the following disclaimer in the
  40.  *    documentation and/or other materials provided with the distribution.
  41.  * 3. All advertising materials mentioning features or use of this software
  42.  *    must display the following acknowledgement:
  43.  *    This product includes software developed by the University of
  44.  *    California, Berkeley and its contributors.
  45.  * 4. Neither the name of the University nor the names of its contributors
  46.  *    may be used to endorse or promote products derived from this software
  47.  *    without specific prior written permission.
  48.  *
  49.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59.  * SUCH DAMAGE.
  60.  */
  61.  
  62. #ifndef lint
  63. char copyright[] =
  64. "@@(#) Copyright (c) 1983 Regents of the University of California.\n\
  65.  All rights reserved.\n";
  66. #endif /* not lint */
  67.  
  68. #ifndef lint
  69. static char sccsid[] = "@@(#)logger.c    6.15 (Berkeley) 3/1/91";
  70. #endif /* not lint */
  71.  
  72. #include <stdio.h>
  73. #define    SYSLOG_NAMES
  74. #include <syslog.h>
  75. #include <ctype.h>
  76.  
  77. /*
  78. **  LOGGER -- read and log utility
  79. **
  80. **    This routine reads from an input and arranges to write the
  81. **    result on the system log, along with a useful tag.
  82. */
  83.  
  84. main(argc, argv)
  85.     int argc;
  86.     char **argv;
  87. {
  88.     extern char *optarg;
  89.     extern int errno, optind;
  90.     int pri = LOG_NOTICE;
  91.     int ch, logflags = 0;
  92.     char *tag, buf[1024], *getlogin(), *strerror();
  93.  
  94.     tag = NULL;
  95.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  96.         switch((char)ch) {
  97.         case 'f':        /* file to log */
  98.             if (freopen(optarg, "r", stdin) == NULL) {
  99.                 (void)fprintf(stderr, "logger: %s: %s.\n",
  100.                     optarg, strerror(errno));
  101.                 exit(1);
  102.             }
  103.             break;
  104.         case 'i':        /* log process id also */
  105.             logflags |= LOG_PID;
  106.             break;
  107.         case 'p':        /* priority */
  108.             pri = pencode(optarg);
  109.             break;
  110.         case 's':        /* log to standard error */
  111.             logflags |= LOG_PERROR;
  112.             break;
  113.         case 't':        /* tag */
  114.             tag = optarg;
  115.             break;
  116.         case '?':
  117.         default:
  118.             usage();
  119.         }
  120.     argc -= optind;
  121.     argv += optind;
  122.  
  123.     /* setup for logging */
  124.     openlog(tag ? tag : getlogin(), logflags, 0);
  125.     (void) fclose(stdout);
  126.  
  127.     /* log input line if appropriate */
  128.     if (argc > 0) {
  129.         register char *p, *endp;
  130.         int len;
  131.  
  132.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  133.             len = strlen(*argv);
  134.             if (p + len > endp && p > buf) {
  135.                 syslog(pri, "%s", buf);
  136.                 p = buf;
  137.             }
  138.             if (len > sizeof(buf) - 1)
  139.                 syslog(pri, "%s", *argv++);
  140.             else {
  141.                 if (p != buf)
  142.                     *p++ = ' ';
  143.                 bcopy(*argv++, p, len);
  144.                 *(p += len) = '\0';
  145.             }
  146.         }
  147.         if (p != buf)
  148.             syslog(pri, "%s", buf);
  149.         exit(0);
  150.     }
  151.  
  152.     /* main loop */
  153.     while (fgets(buf, sizeof(buf), stdin) != NULL)
  154.         syslog(pri, "%s", buf);
  155.  
  156.     exit(0);
  157. }
  158.  
  159.  
  160. /*
  161.  *  Decode a symbolic name to a numeric value
  162.  */
  163. pencode(s)
  164.     register char *s;
  165. {
  166.     char *save;
  167.     int fac, lev;
  168.  
  169.     for (save = s; *s && *s != '.'; ++s);
  170.     if (*s) {
  171.         *s = '\0';
  172.         fac = decode(save, facilitynames);
  173.         if (fac < 0) {
  174.             (void)fprintf(stderr,
  175.                 "logger: unknown facility name: %s.\n", save);
  176.             exit(1);
  177.         }
  178.         *s++ = '.';
  179.     }
  180.     else {
  181.         fac = 0;
  182.         s = save;
  183.     }
  184.     lev = decode(s, prioritynames);
  185.     if (lev < 0) {
  186.         (void)fprintf(stderr,
  187.             "logger: unknown priority name: %s.\n", save);
  188.         exit(1);
  189.     }
  190.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  191. }
  192.  
  193. decode(name, codetab)
  194.     char *name;
  195.     CODE *codetab;
  196. {
  197.     register CODE *c;
  198.  
  199.     if (isdigit(*name))
  200.         return (atoi(name));
  201.  
  202.     for (c = codetab; c->c_name; c++)
  203.         if (!strcasecmp(name, c->c_name))
  204.             return (c->c_val);
  205.  
  206.     return (-1);
  207. }
  208.  
  209. usage()
  210. {
  211.     (void)fprintf(stderr,
  212.         "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
  213.     exit(1);
  214. }
  215. @
  216.  
  217.  
  218. 1.1
  219. log
  220. @Initial revision
  221. @
  222. text
  223. @d45 1
  224. a130 2
  225. #define    SYSLOG_NAMES
  226. #include <syslog.h>
  227. @
  228.